package com.song.dubbo3consumer.config;


import org.apache.commons.lang.StringUtils;
import org.slf4j.MDC;

import java.util.UUID;

/**
 * @Author SHI YUAN
 * @DATE 2022/2/8 8:48 PM
 * @Version 1.0
 * @Desc
 */
public class TraceUtils {

    private static final String TRACE_ID = "traceId";

    public static void createTraceId(){
        String traceId = MDC.get(TRACE_ID);
        if (StringUtils.isBlank(traceId)){
            traceId = UUID.randomUUID().toString().replaceAll("-", "");
            MDC.put(TRACE_ID, traceId);
        }
    }

    public static String getTraceId(){
        String traceId = MDC.get(TRACE_ID);
        if (StringUtils.isBlank(traceId)){
            traceId = UUID.randomUUID().toString().replaceAll("-", "");
            MDC.put(TRACE_ID, traceId);
        }
        return traceId;
    }

    public static void destroyTraceId(){
        MDC.remove(TRACE_ID);
    }
}
